package com.ph.permission.api.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.dubbo.config.annotation.Reference;
import com.ph.base.BaseController;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.permission.entity.User;
import com.ph.shopping.facade.permission.service.ILoginService;
import com.ph.shopping.facade.permission.vo.SessionRoleVo;
import com.ph.shopping.facade.permission.vo.SessionUserVo;

/**
 * @项目：phshopping-api-platform
 *
 * @描述：登录controller
 *
 * @作者： Mr.chang
 *
 * @创建时间：2017年3月20日
 *
 * @Copyright @2017 by Mr.chang
 */
@Controller
public class LoginController extends BaseController{
	
	@Reference(version="1.0.0")
	private ILoginService loginService;
	
	/**
	 * 默认跳转页
	 * @return
	 */
	@RequestMapping(value="/",method=RequestMethod.GET)
	public String loginPage(){
		return "redirect:login";
	}
	
	/**
	 * 跳转首页
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public ModelAndView index(){
		ModelAndView mv=new ModelAndView("index");
		//返回index需要的用户名和角色信息
		SessionUserVo su=getLoginUser();
		if (!StringUtils.isEmpty(su)) {
			mv.addObject("userId", su.getId());
			mv.addObject("userName", su.getTelphone());
			StringBuffer sb=new StringBuffer();
			if(su.getSessionRoleVo()!=null&&su.getSessionRoleVo().size()>0){
				for (SessionRoleVo sr : su.getSessionRoleVo()) {
					sb.append(sr.getRoleName()).append(",");
				}
				String str=sb.toString();
				String roleNames=str.substring(0, str.length()-1);
				mv.addObject("roleName", roleNames);
			}
		}
		return mv;
	}
	
	/**
	 * 跳转登录
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(){
		return "login";
	}
	
	/**
	 * @执行登录
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/doLogin",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView doLogin(@ModelAttribute User user,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		Result result=loginService.login(user);
		if (result.getCode().equals(ResultEnum.SUCCESS.getCode())) {
			setLoginUser(request, result.getData());
			mv.setViewName("redirect:index");
		}else{
			mv.setViewName("login");
			mv.addObject("code", result.getCode());
			mv.addObject("msg", result.getMessage());
		}
		return mv;
	}

	/**
	 * 注销
	 * @param request
	 * @return String
	 * @author zhegnpeng
	 */
	@RequestMapping(value = "/logout", method=RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		try {
			removeLoginUser(request);
		} catch (Exception e) {
			logger.error("注销登录处理错误", e);
		}
		return "redirect:login";
	}
}
